#pragma once

#include <bondlib/BondInfo.h>
#include <bondlib/BondMarketStream.h>
#include <qbprotocol/include/SSQBModel.h>
#include <qbprotocol/include/SSQBAModel.h>
#include <widgets/chart/basechart_axis.h>
#include <widgets/chart/basechart_layoutitem.h>
#include <QtWidgets/QWidget>
#include <QtWidgets/QVBoxLayout>
#include <QTimer>
#include <QColor>
// #include "bondchart.h"
// #include "bondcharttools.h"

struct BONDRATE {
	double	expire;		//剩余期限，单位年
	double	price;		//成交价格
	const bool operator < (const BONDRATE& obj) const
	{
		return (expire < obj.expire);
	}
};

struct BONDTRADEINFO {
	double	expire;			//剩余期限，单位年
	double	price;			//成交价格
	char	sprice[12];
	int		brokerid;		//经纪商ID
	char	code[64];		//债券代码
	char	key[64];		//债券KEY
	char	shortname[64];	//债券简称
	char	remain[16];		//剩余期限
	char	rating[12];		//主体评级
	char	operate[5];		//交易类型，Token，Given，Trade
	time_t	moditytime;		//更新时间
	BONDTRADEINFO() {
		memset(this, 0, sizeof(BONDTRADEINFO));
	}
	const bool operator < (const BONDTRADEINFO& obj) const
	{
		return (moditytime > obj.moditytime);
	}
};

class CBondTradeInfoList {
public:
	std::list<BONDTRADEINFO> m_List;
public:
	CBondTradeInfoList() {};
	~CBondTradeInfoList() {
		m_List.clear();
	};
};

class BondDealChart
	: public QWidget
	, public BaseChartCustomAxisGrid
	, public BaseChartCustomTextElement
{
	Q_OBJECT
public:
	BondDealChart(QWidget* parent = nullptr);
	~BondDealChart();

	enum emtype {
		type_gov = 0,//利率债
		type_credit  //信用债
	};

	void loadDealData();
	void updateDealData(QList<xQBPanoramicMarketStreamUnit>& lstData);
	// 		void loadData(CAnalysisYieldCurve* pYieldCurve, int remain);
	void setType(emtype type);
	void createChart();
	void refreshData();
	void setLeftDrag(double ratio);
	void setRightDrag(double ratio);
	void setCentralDrag(double ratio1, double ratio2);

	BaseChartDragBarElement* dragBar() { return bcde; }

	// BaseChartCustomAxisGrid
	virtual bool drawGridXLines(BaseChartPainter* painter);
	virtual bool drawGridYLines(BaseChartPainter* painter);

	// BaseChartCustomTextElement
	virtual bool customDraw(BaseChartPainter* painter, const QRect& rect);
signals:
	void mouseDoubleClick(QWidget*);
protected:
	// 		void inputData(CAnalysisYieldCurve* pYieldCurve);
	void clearCurrentCurve();
	void GetShowLimit(double fMinXShow, double fMaxXShow, double& fMinYShow, double& fMaxYShow);
	// 		void DecodeMarketStreamUnit(MarketStreamInfo& stInfo, const xQBMarketStreamUnit_c& unit);
	int GetDate(const char* cDate);
	double getXPos_Value(double value);

	void paintEvent(QPaintEvent* event) override;
	void leaveEvent(QEvent* event) override;

	Q_SLOT void onMouseMove(QMouseEvent* event);

private:
	BaseChartCustomPlot* customPlot = nullptr;
	QVBoxLayout* verticalLayout = nullptr;
	BaseChartTextElement* bcte = nullptr;
	BaseChartDragBarElement* bcde = nullptr;

	emtype m_type;
	std::vector<xQBAYieldCurveList_c> m_vctCurve;

	int							m_lRemain;//剩余期限
	std::list<BONDRATE>			m_CurveShow;
	std::list<BONDTRADEINFO>	m_StreamShow;
	QPointF* m_pLineP = nullptr;
	int							m_nLinePNum;
	QPointF* m_pPoints = nullptr;
	int							m_nPointNum;
	int							m_nHotPoint;
	double						m_dRatioBegin = 0.0, m_dRatioEnd = 1.0;
	double						m_dMinX = 0.0, m_dMaxX = 0.0;
	double						m_dMinY = 0.0, m_dMaxY = 0.0;
	double						m_dOriginMinX = 0.0, m_dOriginMaxX = 0.0;
	double						m_dDragLeft = 0.0, m_dDragRight = 0.0;
	std::vector<double>			m_vctGridX;
	QColor						m_clrInHour, m_clrOutHour;

	QColor m_clrCyan, m_clrVolcano, m_clrGeekBlue, m_clrYellow, m_clrMagenta, m_clrGreen;

};

